package com.tyd.cams.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tyd.cams.R;
import com.tyd.cams.entity.StuCjbean;
import com.tyd.cams.entity.ZycxStuListBeam;
import com.tyd.cams.utils.SPUtils;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.dialog.strategy.IDialogStrategy;
import com.xuexiang.xui.widget.dialog.strategy.impl.MaterialDialogStrategy;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

import static com.xuexiang.xui.XUI.getContext;

//import com.xuexiang.xui.widget.dialog.strategy.IDialogStrategy;

/**
 * @author: TYD
 * @作者: 唐YaDong
 * @date: 2021/3/9 3:04
 * @description：ZycjStuListAdapter
 * @描述：ZycjStuListAdapter
 * @版本：1.0
 */
public class ZycjStuListAdapter extends BaseAdapter {
    private List<ZycxStuListBeam> list;
    private LayoutInflater layoutInflater;
    private Context mContext;

    public ZycjStuListAdapter(Context context, List<ZycxStuListBeam> list) {
        this.layoutInflater = LayoutInflater.from(context);
        this.list = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        Log.e("学生", "list.size()--" + list.size());
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        ZycjViewHolder viewHolder;
//        if (convertView == null) {
//            convertView = layoutInflater.inflate(R.layout.tzgg_list_menu, null);
//            viewHolder = new ZycjViewHolder(convertView);
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (ZycjViewHolder) convertView.getTag();
//        }
        convertView = layoutInflater.inflate(R.layout.stuinfolist, null);
        TextView stuname = convertView.findViewById(R.id.stuname);
        TextView classname = convertView.findViewById(R.id.classname);
        TextView personId = convertView.findViewById(R.id.personId);
        ZycxStuListBeam zycxStuListBeam = list.get(position);
        stuname.setText("" + zycxStuListBeam.getPersonName());
        classname.setText(zycxStuListBeam.getClassName());
        personId.setText("" + zycxStuListBeam.getPersonId());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(mContext, "正在查看 " + zycxStuListBeam.getPersonName() + "成绩", Toast.LENGTH_SHORT).show();
                getXscj(zycxStuListBeam);
                /*    Intent intent = new Intent();
                intent.putExtra("href", skTzgg.getHref());
                intent.setClass(getContext(), TzggActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                getContext().startActivity(intent);*/
            }
        });
        return convertView;
    }

    /*专业成绩查询获取开始*/
    private void getXscj(ZycxStuListBeam zycxStuListBeam) {
        new Thread(() -> OkHttpUtils.post()
                .url(SPUtils.getIP() + "/result/mtgetResul")
                .addHeader("Authorization", SPUtils.getToken())
                .addParams("personId", zycxStuListBeam.getPersonId().toString())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(mContext, "查询成绩超时" + zycxStuListBeam.getPersonName(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("获取的返回信息", response);
                        Gson son = new Gson();
                        List<StuCjbean> stuCjbeans = son.fromJson(response, new TypeToken<List<StuCjbean>>() {
                        }.getType());
//                        Toast.makeText(getContext(), "学生成绩" + stuCjbeans.toString(), Toast.LENGTH_SHORT).show();

                        final EditText editText = new EditText(getContext());
                        for (StuCjbean cjbean : stuCjbeans) {
                            editText.setText(editText.getText() + "课程：" + cjbean.getCourseName() + "\t\t" + "成绩:" + cjbean.getScore() + "\n\n\t");
                            editText.setEnabled(false);
                        }
                        /*new ClubDialog(mContext, "", new ClubDialog.OnClicktener() {
                            @Override
                            public void onClick() {

                            }
                        }).show();*/
                        //设置对话框策略
                        IDialogStrategy mDialogLoader = DialogLoader.getInstance().setIDialogStrategy(new MaterialDialogStrategy());
                        mDialogLoader.showTipDialog(mContext,
                                R.drawable.cjcx,
                                zycxStuListBeam.getPersonName() + "的成绩",
                                editText.getText().toString(),
                                "确认",
                                (dialog, which) -> {
                                    //点击确认按钮后关闭提示框
                                    dialog.dismiss();
                                });
                                /*AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
                                .setTitle(zycxStuListBeam.getPersonName() + "的成绩")
                                .setView(editText)
                                .setIcon(R.drawable.cjcx)
                                .setCancelable(true)
                                .setPositiveButton("返回", (dialog, which) -> dialog.dismiss())
                                .setNegativeButton("", (dialog, which) -> dialog.dismiss());
                        alertDialog=builder.create();
                        alertDialog.setCanceledOnTouchOutside(true);
                        alertDialog.show();*/
//                        Toast.makeText(mContext, "学生成绩" + stuCjbeans.toString(), Toast.LENGTH_SHORT).show();
                    }
                })).start();
    }

}
